package api

import (
	"bbs-go/internal/models"
	"bbs-go/internal/services"
	"strconv"

	"github.com/kataras/iris/v12"
	"github.com/mlogclub/simple/web"
	"github.com/mlogclub/simple/web/params"
)

type IndexArticleController struct {
	Ctx iris.Context
}

func (c *IndexArticleController) GetBy(id int64) *web.JsonResult {
	ti := models.IndexArticleInfo{}
	t := services.IndexArticleService.Get(id)
	if t == nil {
		return web.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
	}
	if t != nil {
		ti.Id = t.Id
		ti.CreatedAt = t.CreatedAt.Format("2006-01-02 15:04:05")
		ti.IndexCategoryId = t.IndexCategoryId
		ti.UserId = t.UserId
		ti.Title = t.Title
		ti.Summary = t.Summary
		ti.ContentType = t.ContentType
		ti.Content = t.Content
		ti.Cover = t.Cover
		ti.Sort = t.Sort
		ti.Status = t.Status
		ti.Source = t.Source
		ti.SourceUrl = t.SourceUrl
		ti.ViewCount = t.ViewCount
		ti.LikeCount = t.LikeCount
		ti.IndexShow = t.IndexShow
	}
	ic := services.IndexCategoryService.Get(t.IndexCategoryId)
	if ic != nil {
		ti.IndexCategoryName = ic.Name
	}
	return web.JsonData(ti)
}

func (c *IndexArticleController) AnyList() *web.JsonResult {
	p := params.NewQueryParams(c.Ctx)
	p = p.PageByReq()
	indexCategoryId, err := c.Ctx.URLParamInt("indexCategoryId")
	if err != nil {
		return web.JsonError(err)
	}

	list, err := services.IndexArticleService.FindSimpleList(&models.IndexArticleSimpleRequest{
		Limit:           p.Paging.Limit,
		Page:            p.Paging.Page,
		IndexCategoryId: indexCategoryId,
	})
	if err != nil {
		return web.JsonError(err)
	}
	return web.JsonData(list)
}

func (c *IndexArticleController) AnyIndexContent() *web.JsonResult {
	resp, err := services.IndexArticleService.FindIndexContent()
	if err != nil {
		return web.JsonError(err)
	}
	return web.JsonData(resp)
}

func (c *IndexArticleController) AnyIndexContentV2() *web.JsonResult {
	resp, err := services.IndexArticleService.FindIndexContentV2()
	if err != nil {
		return web.JsonError(err)
	}
	return web.JsonData(resp)
}

func (c *IndexArticleController) AnyNewsList() *web.JsonResult {
	p := params.NewQueryParams(c.Ctx)
	p = p.PageByReq()
	indexCategoryId := 11
	list, err := services.IndexArticleService.FindSimpleList(&models.IndexArticleSimpleRequest{
		Limit:           p.Paging.Limit,
		Page:            p.Paging.Page,
		IndexCategoryId: indexCategoryId,
	})
	if err != nil {
		return web.JsonError(err)
	}
	return web.JsonData(list)
}

func (c *IndexArticleController) AnySystemNoticeList() *web.JsonResult {
	p := params.NewQueryParams(c.Ctx)
	p = p.PageByReq()
	indexCategoryId := 12
	list, err := services.IndexArticleService.FindSimpleList(&models.IndexArticleSimpleRequest{
		Limit:           p.Paging.Limit,
		Page:            p.Paging.Page,
		IndexCategoryId: indexCategoryId,
	})

	if err != nil {
		return web.JsonError(err)
	}
	return web.JsonData(list)
}
